<!--  -->
<template>
  <div class='xinzhengdizhi'>
      <van-nav-bar
      :title="fdfffffddf ? '新增地址' : '保存地址' "
      left-text="返回"
        
      @click-left="$router.back()"
    >
    </van-nav-bar>
      <van-address-edit
  :area-list="areaList"
  :address-info="addressInfo"
  :show-delete="!fdfffffddf"
  show-set-default
  
   @save="onSave"
   @delete="onDelete"
    
  
  
  
/>
  </div>
</template>

<script>
import { areaList } from '@vant/area-data'
import { xinzengdizhiqq ,xiugaidizhi,shanchudizhi} from "network/api.js";
import { mapMutations ,mapState} from "vuex";
export default {
name:'',
  data () {
    return {
      areaList ,
     
    };
  },


  computed: {
    ...mapState(['userInfo']),
      fdfffffddf(){
        return  !!(this.$route.query.vv=='xize')
      },
      //添加默认值
      addressInfo(){
        if(!this.fdfffffddf){
          let {addressList}=this.userInfo
        let fdfff=addressList.filter(item=>{
        
          return item.id==this.$route.query.vv})[0]
        console.log(fdfff)
        let {id,name,mobile:tel,province,city,country:county,detail:addressDetail} =fdfff
        return {id,name,tel,province,city,county,addressDetail}
        }else{
          return {}
        }
        
      }
  },


  methods: {
    ...mapMutations(['zzjdz']),
    //保存
   async onSave(content){ 
     console.log(content)    

      let{name,province,city,country,addressDetail:detail,tel:mobile,areaCode:code}=content
      let dfdfdfgg={name,province,city,country,detail,mobile,code,default:0}
      if(this.fdfffffddf){
         const {errcode,data}=await xinzengdizhiqq(dfdfdfgg)
      if(errcode!=0){
         this.$toast('添加失败');
      }else{
        console.log(data)
      this.zzjdz(data)
      this.$router.back()
      }
      
    }else{
       const {errcode,data}=await xiugaidizhi(this.$route.query.vv,dfdfdfgg)
      if(errcode!=0){
         this.$toast('添加失败');
      }
      // const dfdfdfgg
      dfdfdfgg.id=this.$route.query.vv
      console.log(dfdfdfgg)
      this.zzjdz(dfdfdfgg)
      this.$router.back()
    }
      },
     //删除
    async onDelete(){
      const {errcode}=await shanchudizhi(this.$route.query.vv)
      if(errcode!=0){
        this.$toast('删除失败');
      }else{
        this.zzjdz(this.$route.query.vv)
        this.$router.back()
      }

      
    }
  },
  created(){

  }
}

</script>
<style lang='less' scoped>
</style>